package com.cqq.auth.shiro;

import org.apache.shiro.web.filter.mgt.FilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import java.util.ArrayList;
import java.util.List;

public class CustomPathMatchingFilterChainResolver extends
		PathMatchingFilterChainResolver {

	private static final Logger log = LoggerFactory.getLogger(CustomPathMatchingFilterChainResolver.class);
	
	private CustomDefaultFilterChainManager customDefaultFilterChainManager;

	public void setCustomDefaultFilterChainManager(
			CustomDefaultFilterChainManager customDefaultFilterChainManager) {
		this.customDefaultFilterChainManager = customDefaultFilterChainManager;
		setFilterChainManager(customDefaultFilterChainManager);
	}

	public FilterChain getChain(ServletRequest request,
			ServletResponse response, FilterChain originalChain) {
		FilterChainManager filterChainManager = getFilterChainManager();
		if (!filterChainManager.hasChains()) {
			return null;
		}

		String requestURI = getPathWithinApplication(request);
		List<String> chainNames = new ArrayList<String>();
		for (String pathPattern : filterChainManager.getChainNames()) {
			if (pathMatches(pathPattern, requestURI)) {
				chainNames.add(pathPattern);
				log.debug("Matched path pattern [{}] for requestURI [{}]",pathPattern,requestURI);
			}
		}

		if (chainNames.size() == 0) {
			return null;
		}
		return customDefaultFilterChainManager.proxy(originalChain, chainNames);
	}
}
